package com.mew.chess.core.game.bjl.game.core;

import com.mew.chess.core.game.bjl.game.BjlRoomManager;
import com.mew.chess.core.game.bjl.game.common.BjlBetType;
import com.mew.chess.core.game.bjl.game.common.BjlResultBO;
import com.mew.common.enm.UserType;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dylin
 * @create 2021/4/25
 */
public class BjlGameUtil {
    /**
     * 验证输赢
     */
    public static void validWin(BjlResultBO resultBO){
        validBankerIdleOREqual(resultBO);
        if(validTwo(resultBO.getBankerCards())){
            resultBO.getWinList().add(BjlBetType.BankerTwo.code());
        }
        if(validTwo(resultBO.getIdleCards())){
            resultBO.getWinList().add(BjlBetType.IdleTwo.code());
        }
    }

    /**
     * 验证庄闲和
     */
    private static void validBankerIdleOREqual(BjlResultBO resultBO){
        int bankerVal=BjlCardUtil.computeCardsVal(resultBO.getBankerCards());
        int idleVal=BjlCardUtil.computeCardsVal(resultBO.getIdleCards());
        if(idleVal>bankerVal){
            resultBO.getWinList().add(BjlBetType.IdleWin.code());
        }else if(idleVal==bankerVal){
            resultBO.getWinList().add(BjlBetType.Equal.code());
        }else{
            resultBO.getWinList().add(BjlBetType.BankerWin.code());
        }
    }

    /**
     * 验证对子
     * @return
     */
    private static boolean validTwo(List<Integer> cards){
        Integer c1=cards.get(0)%13;
        Integer c2=cards.get(1)%13;
        return c1.equals(c2);
    }

    /**
     * 计算区域压注
     */
    public static Map<Integer,Long> computeRegionBet(BjlRoomManager roomManager){
        Map<Integer,Long> regionBetMap = new HashMap<>();
        roomManager.getPlayerList().values().stream().filter(u -> u.userType != UserType.Robot.code() && u.getAllBet() > 0).forEach(u -> {
            for (Map.Entry<Integer, long[]> entry : u.getBetRecord().entrySet()) {
                Long allBet = regionBetMap.get(entry.getKey());
                allBet = allBet == null ? 0l : allBet;
                regionBetMap.put(entry.getKey(), allBet+entry.getValue()[0]);
            }
        });
        return regionBetMap;
    }
}
